package com.roc.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.roc.dao.DaoException;
import com.roc.dao.black.IBlackDao;
import com.roc.pojo.BlackList;
import com.roc.util.Constance;

@Repository
public class BlackDaoImpl extends BaseDaoImpl implements IBlackDao{

	@Override
	public BlackList queryByNo(String acctNo, String cardNo, String licenseNo)
			throws DaoException {
		// TODO Auto-generated method stub
		BlackList black=null;
		Session session=sessionFactory.getCurrentSession();
		try{
			String hql="";
			StringBuffer sb=new StringBuffer("");
			sb.append("from BlackList b where ");
			if(Constance.isNotEmpty(acctNo)){
				sb.append(" b.acctNo=:acctNo and");
			}
			if(Constance.isNotEmpty(cardNo)){
				sb.append(" b.cardNo=:cardNo and");
			}
			if(Constance.isNotEmpty(licenseNo)){
				sb.append(" b.licenseNo=:licenseNo and");
			}
			
			hql=sb.substring(0,sb.length()-3);

			Query query=session.createQuery(hql);
			
			if(Constance.isNotEmpty(acctNo)){
				query.setString("acctNo", acctNo);
			}
			if(Constance.isNotEmpty(cardNo)){
				query.setString("cardNo", cardNo);
			}
			if(Constance.isNotEmpty(licenseNo)){
				query.setString("licenseNo", licenseNo);
			}
			black=(BlackList) query.uniqueResult();
			
		}catch(HibernateException e){
			//logger.error(e.getMessage(), e);
			throw new DaoException("查询唯一黑名单失败");
		}
		return black;
	}

}
